﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using RhotCMS.Models;
using RhotCMS.Controllers;
using Core.Constants;

namespace RhotCMS.Filters
{
    /// <summary>
    /// Use for Localization actions
    /// </summary>
    public class LocalizationAttribute : ActionFilterAttribute
    {
        private const string STR_RuRU = ConstantsLanguages.STR_DEFAULT_LANGUAGE;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var langHeader = string.Empty;

            if (!Convert.ToBoolean(CMSSettingsRepository.GetSingleAppSetting("IsMultiLanguage")))
            {
                langHeader = STR_RuRU;
                Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
                return;
            }

            if (filterContext.RouteData.Values["lang"] != null &&
                  !string.IsNullOrWhiteSpace(filterContext.RouteData.Values["lang"].ToString()) && filterContext.RouteData.Values["lang"].ToString() != "favicon.ico")
            {
                // set the culture from the route data (url)
                var lang = filterContext.RouteData.Values["lang"].ToString();                
                try
                {
                    var culture = CultureInfo.CreateSpecificCulture(lang);
                    Thread.CurrentThread.CurrentUICulture = culture;
                    langHeader = culture.Name;
                }
                catch
                {
                    langHeader = STR_RuRU;
                }
            }
            else
            {
                // load the culture info from the cookie
                var cookie = filterContext.HttpContext.Request.Cookies[String.Format("{0}.CurrentUICulture", CMSSettingsRepository.GetSingleAppSetting("SiteName"))];
                if (cookie != null && !String.IsNullOrWhiteSpace(cookie.Value))
                {
                    // set the culture by the cookie content
                    langHeader = cookie.Value;
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
                }
                else
                {
                    // set the culture by the location if not speicified
                    try
                    {
                        var userLang = filterContext.HttpContext.Request.UserLanguages[0];
                        if (userLang != null)
                        {
                            if (userLang == STR_RuRU)
                                langHeader = userLang;
                            else
                                langHeader = "en-US";
                        }
                        else
                        {
                            langHeader = STR_RuRU;
                        }
                    }
                    catch
                    {
                        langHeader = STR_RuRU;
                    }

                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(langHeader);
                }
                // set the lang value into route data
                filterContext.RouteData.Values["lang"] = langHeader;
            }

            // save the location into cookie
            var newCookie = new HttpCookie(String.Format("{0}.CurrentUICulture", CMSSettingsRepository.GetSingleAppSetting("SiteName")));
            newCookie.Value = langHeader;
            newCookie.Expires = DateTime.Now.AddYears(1);
            filterContext.HttpContext.Response.SetCookie(newCookie);

            RhotCMSController controller = filterContext.Controller as RhotCMSController;
            if (controller != null)
                controller.UILangName = Thread.CurrentThread.CurrentUICulture.Name;

            base.OnActionExecuting(filterContext);
        }
    }
}
